﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rubik.Common.Operations
{
    public class LeftRotateClockwise : ICubeOperation
    {
        private readonly int level;

        public LeftRotateClockwise(int level)
        {
            this.level = level;
        }

        private readonly ElementPositionTransform[,] transformationMatrix = new[,]
            {
                { new ElementPositionTransform(0, 0, 2), new ElementPositionTransform(0, 1, 1), new ElementPositionTransform(0, 2, 0) },
                { new ElementPositionTransform(0, -1, 1), new ElementPositionTransform(0, 0, 0), new ElementPositionTransform(0, 1, -1) },
                { new ElementPositionTransform(0, -2, 0), new ElementPositionTransform(0, -1, -1), new ElementPositionTransform(0, 0, -2) }
            };

        public void Do(Cube cube)
        {
            CubeElement[,,] copy = (CubeElement[,,])cube.Elements.Clone();
            for (int i = 0; i < Cube.Scale; i++)
            {
                for (int j = 0; j < Cube.Scale; j++)
                {
                    int zShift = transformationMatrix[j, i].ZDelta;
                    int yShift = transformationMatrix[j, i].YDelta;
                    copy[level, j + yShift, Cube.Scale - 1 - i - zShift] = cube.Elements[level, j, Cube.Scale - 1 - i];
                    copy[level, j + yShift, Cube.Scale - 1 - i - zShift].Rotate(RotateAxis.X, true);
                }
            }
            cube.Elements = copy;
        }
    }
}
